//
//  SGResultViewController.m
//  Fengshui
//
//  Created by are on 13-5-23.
//
//

#import "SGResultViewController.h"
#import "TextViewController.h"
@interface SGResultViewController ()

@end

@implementation SGResultViewController
@synthesize currentPage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (SinaWeibo *)sinaweibo
{
    AppDelegate  *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.sinaweibo.delegate = self;
    return delegate.sinaweibo;
}
-(IBAction)shareBtnAction:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择设备"  delegate:self
                                              cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    
    [sheet addButtonWithTitle:@"分享到新浪微博"];
    [sheet addButtonWithTitle:@"分享到微信"];
    [sheet addButtonWithTitle:@"分享到朋友圈"];
    [sheet addButtonWithTitle:@"取消"];
    // [sheet addButtonWithTitle:@"本地播放"];
    [sheet showFromRect:self.view.bounds inView:self.view animated:YES];

}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    

        if (buttonIndex == 0) {

            AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];
            
            [userInfo release], userInfo = nil;
            [statuses release], statuses = nil;
            SinaWeibo *sinaweibo = [self sinaweibo];
            [sinaweibo logIn];
            

        }
        else if (buttonIndex == 1)
        {
            AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];
            delegate.sinaOrWeixin = YES;
            _scene = WXSceneSession;
            NSString *string = [NSString string];
            for (NSInteger i = 0; i<[self.resultArray count]; i++) {

                string = [string stringByAppendingFormat:@"%@",[[self.resultArray objectAtIndex:currentPage] objectForKey:@"desc"]];
            }
            
            [self sendTextContent:string];
        }
        else if (buttonIndex == 2)
        {
            AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];
            
            delegate.sinaOrWeixin = YES;
            _scene = WXSceneTimeline;
            NSString *string = [NSString string];
            for (NSInteger i = 0; i<[self.resultArray count]; i++) {

                string = [string stringByAppendingFormat:@"%@",[[self.resultArray objectAtIndex:i] objectForKey:@"desc"]];
            }
            
            [self sendTextContent:string];
        }
        
    
}
-(void)viewWillAppear:(BOOL)animated
{

    
}
#pragma weixin
- (void)doAuth
{
    SendAuthReq* req = [[[SendAuthReq alloc] init] autorelease];
    req.scope = @"post_timeline";
    req.state = @"xxx";

    [WXApi sendReq:req];
}

-(void) changeScene:(NSInteger)scene{
    _scene = scene;
}

-(void) onSentTextMessage:(BOOL) bSent
{
    // 通过微信发送消息后， 返回本App
    NSString *strTitle = [NSString stringWithFormat:@"发送结果"];
    NSString *strMsg = [NSString stringWithFormat:@"发送文本消息结果:%u", bSent];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

-(void) onSentMediaMessage:(BOOL) bSent
{
    // 通过微信发送消息后， 返回本App
    NSString *strTitle = [NSString stringWithFormat:@"发送结果"];
    NSString *strMsg = [NSString stringWithFormat:@"发送媒体消息结果:%u", bSent];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
- (void) sendTextContent:(NSString*)nsText
{
    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = YES;
    req.text = nsText;
    req.scene = _scene;

    [WXApi sendReq:req];
}

#pragma ----
- (void)viewDidLoad
{
    [super viewDidLoad];
    _scene = WXSceneSession;
    currentPage = 0;
    UILabel *label = (UILabel *)[self.resultView viewWithTag:1];

    label.text = [NSString stringWithFormat:@"%@(%d/%d)",[[self.resultArray objectAtIndex:currentPage] objectForKey:@"title"],currentPage+1,self.resultArray.count];

    UITextView *textView = (UITextView *)[self.resultView viewWithTag:2];
    textView.text = [[self.resultArray objectAtIndex:0] objectForKey:@"desc"];
    [textView setEditable:NO];


    self.lastPageButton.hidden = YES;
    self.shareBtn.hidden = YES;
    if (self.resultArray.count==1) {

        self.shareBtn.hidden = NO;
        self.nextPageButton.hidden = YES;
    }
        // Do any additional setup after loading the view from its nib.
}
-(IBAction)lookLastPage:(id)sender
{
    self.nextPageButton.hidden = NO;
    currentPage = currentPage -1;
    self.shareBtn.hidden = YES;
    if (currentPage == 0) {

        self.lastPageButton.hidden = YES;
        
    }
    UILabel *label = (UILabel *)[self.resultView viewWithTag:1];

   label.text = [NSString stringWithFormat:@"%@(%d/%d)",[[self.resultArray objectAtIndex:currentPage] objectForKey:@"title"],currentPage+1,self.resultArray.count];
    
    UITextView *textView = (UITextView *)[self.resultView viewWithTag:2];
    textView.text = [[self.resultArray objectAtIndex:currentPage] objectForKey:@"desc"];
    [textView setEditable:NO];

}
-(IBAction)looknextPage:(id)sender
{
    
    self.lastPageButton.hidden = NO;
    currentPage = currentPage +1;
    if (currentPage == self.resultArray.count-1) {

        self.nextPageButton.hidden = YES;
        self.shareBtn.hidden = NO;
    }
    UILabel *label = (UILabel *)[self.resultView viewWithTag:1];

    label.text = [NSString stringWithFormat:@"%@(%d/%d)",[[self.resultArray objectAtIndex:currentPage] objectForKey:@"title"],currentPage+1,self.resultArray.count];

    UITextView *textView = (UITextView *)[self.resultView viewWithTag:2];
    textView.text = [[self.resultArray objectAtIndex:currentPage] objectForKey:@"desc"];
    [textView setEditable:NO];

    
}
-(void)setDataArray:(NSArray *)array
{
    self.resultArray = [NSArray arrayWithArray:array];
    
}
- (void)storeAuthData
{
    SinaWeibo *sinaweibo = [self sinaweibo];
    
    NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys:
                              sinaweibo.accessToken, @"AccessTokenKey",
                              sinaweibo.expirationDate, @"ExpirationDateKey",
                              sinaweibo.userID, @"UserIDKey",
                              sinaweibo.refreshToken, @"refresh_token", nil];
    [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
#pragma -----sinaWeiBoDelegate
- (void)request:(SinaWeiboRequest *)request didReceiveResponse:(NSURLResponse *)response
{
    
}
- (void)request:(SinaWeiboRequest *)request didReceiveRawData:(NSData *)data
{
    
}
- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
    Dlog(@"%@",error);
}
- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result
{
    Dlog(@"%@",result);
}
- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo
{
    
    [self storeAuthData];
    NSString *string = [NSString string];
    for (NSInteger i = 0; i<[self.resultArray count]; i++) {

        string = [string stringByAppendingFormat:@"%@",[[self.resultArray objectAtIndex:i] objectForKey:@"desc"]];
    }

    TextViewController* viewController = [[[TextViewController alloc] init] autorelease];
    viewController.m_nsLastText = string;
    UINavigationController *navigatitonController = [[[UINavigationController alloc]initWithRootViewController:viewController] autorelease];
	[self presentModalViewController:navigatitonController animated:YES];
    
}
- (void)sinaweiboDidLogOut:(SinaWeibo *)sinaweibo
{
    
}
- (void)sinaweiboLogInDidCancel:(SinaWeibo *)sinaweibo
{
    
}
- (void)sinaweibo:(SinaWeibo *)sinaweibo logInDidFailWithError:(NSError *)error
{
    
}
- (void)sinaweibo:(SinaWeibo *)sinaweibo accessTokenInvalidOrExpired:(NSError *)error
{
    
}
#pragma -------
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
